# !/usr/bin/env python
# coding=utf-8
# @Time    : 2018/7/13 17:57
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/rpc_dao.git
# @File    : car_raw_info.py
# @Software: PyCharm
# @Version: 2.7.5
from . import Session
from .models import TCarRawinfo


def save_car_raw_info(*args):
	session = Session()
	try:
		session.execute(
			TCarRawinfo.__table__.insert().prefix_with('IGNORE'),
			{'licenseNo': args[0],
			 'vehicleFrameNo': args[1],
			 'engine_code': args[2],
			 'model': args[3],
			 'brand': args[4],
			 'register_date': args[5],
			 'owner_name': args[6],
			 'owner_code': args[7],
			 'owner_mobile': args[8],
			 'trans_date': args[9],
			 'is_trans': args[10]}
		)
		session.commit()
	except Exception:
		session.rollback()


def get_car_raw_info_by_license(license):
	session = Session()
	return session.query(TCarRawinfo).filter_by(licenseNo=license).first()


def get_car_raw_info_by_vin(vin):
	session = Session()
	return session.query(TCarRawinfo).filter_by(vehicleFrameNo=vin).first()


def get_car_owner_info(license, vin):
	session = Session()
	return session.query(TCarRawinfo).filter_by(licenseNo=license, vehicleFrameNo=vin).first()


def update_car_raw_info_by_license(license, *args):
	session = Session()
	try:
		session.query(TCarRawinfo).filter_by(licenseNo=license).update({
			TCarRawinfo.vehicleFrameNo: args[0],
			TCarRawinfo.engine_code: args[1],
			TCarRawinfo.model: args[2],
			TCarRawinfo.brand: args[3],
			TCarRawinfo.register_date: args[4],
			TCarRawinfo.trans_date: args[5],
			TCarRawinfo.is_trans: args[6],
		})
		session.commit()
	except Exception:
		session.rollback()


def update_car_owner_info(license, vin, *args):
	session = Session()
	try:
		session.query(TCarRawinfo).filter_by(licenseNo=license, vehicleFrameNo=vin).update({
			TCarRawinfo.owner_name: args[0],
			TCarRawinfo.owner_code: args[1],
			TCarRawinfo.owner_mobile: args[2],
		})
		session.commit()
	except Exception:
		session.rollback()

